﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Model;
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
namespace WpfApp
{
    /// <summary>
    /// 对PDF 表格的操作
    /// </summary>
    public  class PDFHelper
    {



        public static PdfGrid Write(List<ExamInee> tempList,Group groupData,String groupName)
        {
            PdfGrid grid = new PdfGrid();
            //设置单元格边距和表格默认字体
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            grid.Style.Font = new PdfTrueTypeFont(new Font("宋体", 12f), true);
            grid.Columns.Add(5);



            //时间计算规则 只要 结束时间是12点以及之后都是下午场次
            DateTime beginDT = Util.CTime.GetDateTime(Convert.ToInt32(groupData.begin_time));

            var groupBeginTime = string.Format("{0:yyyy年MM月dd日}", beginDT) + "   " + groupData.time_period;


            //标题
            PdfGridRow titleRow = grid.Rows.Add();
            titleRow.Style.Font = new PdfTrueTypeFont(new Font("宋体", 20f, FontStyle.Bold), true);

            titleRow.Cells[0].ColumnSpan = 5;  //从0行合并行单元格
            titleRow.Cells[0].Value = "抽签顺序表";
            titleRow.Height = 35f;
            titleRow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            titleRow.Cells[0].Style.Borders = new PdfBorders
            {
                All = new PdfPen(Color.White, 0),
                //Top = new PdfPen(System.Drawing.Color.Black, 1)
            };


        

            PdfGridRow titleInfoRow = grid.Rows.Add();
            titleInfoRow.Style.Font = new PdfTrueTypeFont(new Font("宋体", 12f, FontStyle.Bold), true);

            titleInfoRow.Cells[0].ColumnSpan = 4;  //从0行合并行单元格
            titleInfoRow.Cells[0].Value = groupName + "   第_________考场";

            titleInfoRow.Cells[4].ColumnSpan = 1;  //从0行合并行单元格
            titleInfoRow.Cells[4].Value = groupBeginTime;

            titleInfoRow.Height = 20f;
            //titleInfoRow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            titleInfoRow.Cells[0].Style.Borders = new PdfBorders
            {
                All = new PdfPen(System.Drawing.Color.White, 0),
                //Top = new PdfPen(System.Drawing.Color.Black, 1)
            };
            titleInfoRow.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            titleInfoRow.Cells[4].Style.Borders = new PdfBorders
            {
                All = new PdfPen(System.Drawing.Color.White, 0),
                //Top = new PdfPen(System.Drawing.Color.Black, 1)
            };



            PdfGridRow row = grid.Rows.Add();

            row.Style.Font = new PdfTrueTypeFont(new Font("宋体", 13f, FontStyle.Bold), true);


            int nameIndex = 0;
            int numberIndex = 1;
            int qzIndex = 2;
            int zkIndex = 2;
            int jobIndex = 3;
            int companyIndex = 4;
            
            


            //设置列宽
            foreach (PdfGridColumn col in grid.Columns)
            {
                col.Width = 75f;
            }
            

            grid.Columns[nameIndex].Width = 75f;
            grid.Columns[zkIndex].Width = 75f;
            grid.Columns[jobIndex].Width = 115f;
            grid.Columns[companyIndex].Width = 180f;
            grid.Columns[numberIndex].Width = 60f;
            //grid.Columns[qzIndex].Width = 60f;


            row.Cells[nameIndex].Value = "姓名";
            row.Cells[zkIndex].Value = "准考证号";
            //row.Cells[qzIndex].Value = "考生签字";
            row.Cells[jobIndex].Value = "招录职位";
            row.Cells[companyIndex].Value = "招录单位";
            row.Cells[numberIndex].Value = "抽签号";

            row.Cells[2].ColumnSpan = 1;
            row.Cells[3].ColumnSpan = 1;
            row.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            //row.Cells[qzIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[zkIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[jobIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[numberIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);



          
            for (int i = 0; i < tempList.Count; i++)
            {
                //Random rd = new Random();

                //int number = rd.Next(1, 10);


                ////处理招录单位名称
                //tempList[i].company = "太原";

                //for (int j = 0; j < number; j++)
                //{
                //    tempList[i].company += "图";
                //    tempList[i].job_name += "铠";
                //}
                //tempList[i].company += "结";
                //tempList[i].job_name += "结";
                //tempList[i].job_name = "行业工会联合会专干网资专干网资讯哈哈微雾";
                //tempList[i].admission_ticket_no = "123456789123456789";

                PdfGridRow rowTemp = grid.Rows.Add();
                rowTemp.Style.Font = new PdfTrueTypeFont(new Font("宋体", 11f, System.Drawing.FontStyle.Regular), true);

                rowTemp.Cells[nameIndex].Value = tempList[i].name;
                rowTemp.Cells[zkIndex].Value = tempList[i].admission_ticket_no;
                rowTemp.Cells[jobIndex].Value = tempList[i].job_name;
                rowTemp.Cells[companyIndex].Value = tempList[i].company;

              


                //rowTemp.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("宋体", 12f), true);
                if ("2".Equals(tempList[i].result))
                {
                    rowTemp.Cells[numberIndex].Value = tempList[i].no+"【违纪】";
                }
                else if("3".Equals(tempList[i].result))
                {
                    rowTemp.Cells[numberIndex].Value = tempList[i].no + "【弃考】";
                }
                else if ("4".Equals(tempList[i].result))
                {
                    rowTemp.Cells[numberIndex].Value = tempList[i].no + "【其他异常】";
                    rowTemp.Cells[numberIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 8f, FontStyle.Regular), true);

                }
                else
                {
                    rowTemp.Cells[numberIndex].Value = tempList[i].no;
                }


                rowTemp.Cells[jobIndex].ColumnSpan = 1;
                rowTemp.Cells[companyIndex].ColumnSpan = 1;

                rowTemp.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle);
                rowTemp.Cells[zkIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                rowTemp.Cells[jobIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Top);
                rowTemp.Cells[numberIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);


                //处理姓名
                var name_len = tempList[i].name.Length;

                if (name_len > 5 && name_len < 8)
                {
                    rowTemp.Cells[nameIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 10f, FontStyle.Regular), true);
                    rowTemp.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                }
                else if (name_len > 8 && name_len < 12)
                {
                    rowTemp.Cells[nameIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 8f, FontStyle.Regular), true);
                    rowTemp.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (name_len > 12 && name_len < 17)
                {
                    rowTemp.Cells[nameIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 6f, FontStyle.Regular), true);
                    rowTemp.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (name_len >= 17)
                {
                    rowTemp.Cells[nameIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 5f, FontStyle.Regular), true);
                    rowTemp.Cells[nameIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }



                //处理签好
                var examinee_no_len = tempList[i].examinee_no.Length;

                if (examinee_no_len >= 10)
                {
                    rowTemp.Cells[zkIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 10f, FontStyle.Regular), true);
                    rowTemp.Cells[zkIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }

                if (examinee_no_len >= 15)
                {
                    rowTemp.Cells[zkIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 8f, FontStyle.Regular), true);
                    rowTemp.Cells[zkIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }



                //处理职位名称
                var job_name_len = tempList[i].job_name.Length;

                if (job_name_len > 9 && job_name_len < 13)
                {
                    rowTemp.Cells[jobIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 10.5f, FontStyle.Regular), true);
                    rowTemp.Cells[jobIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                }
                else if (job_name_len > 9 && job_name_len < 16)
                {
                    rowTemp.Cells[jobIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 9.5f, FontStyle.Regular), true);
                    rowTemp.Cells[jobIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (job_name_len > 16)
                {
                    rowTemp.Cells[jobIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 7.5f, FontStyle.Regular), true);
                    rowTemp.Cells[jobIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }


                //处理招录单位名称
                var len = tempList[i].company.Length;

                if (len <= 14)
                {
                    rowTemp.Cells[companyIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 12f, FontStyle.Regular), true);
                    rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (len > 14 && len <= 16)
                {
                    rowTemp.Cells[companyIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 10f, FontStyle.Regular), true);
                    rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (len > 16 && len <= 18)
                {
                    rowTemp.Cells[companyIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 9.5f, FontStyle.Regular), true);
                    rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else if (len > 18 && len <= 20)
                {
                    rowTemp.Cells[companyIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 9f, FontStyle.Regular), true);
                    rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                }
                else
                {
                    rowTemp.Cells[companyIndex].Style.Font = new PdfTrueTypeFont(new Font("宋体", 8f, FontStyle.Regular), true);
                    rowTemp.Cells[companyIndex].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Top);
                }




                //默认高度
                //rowTemp.Height = 25.6f;
                rowTemp.Height = 25f;

            }

            PdfGridRow rowTemp2 = grid.Rows.Add();

            rowTemp2.Cells[0].Value = "组织员签字：______________________";

            rowTemp2.Cells[0].ColumnSpan = 3;
            rowTemp2.Cells[3].ColumnSpan = 2;



            rowTemp2.Height = 100f;

            rowTemp2.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            rowTemp2.Cells[0].Style.Borders = new PdfBorders
            {
                All = new PdfPen(Color.White, 0),
                Top = new PdfPen(Color.Black, 1)
            };


            rowTemp2.Cells[3].Value = groupBeginTime;

            rowTemp2.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            rowTemp2.Cells[3].Style.Borders = new PdfBorders
            {
                All = new PdfPen(Color.White, 0),
                Top = new PdfPen(Color.Black, 1)
            };


            return grid;
        }

        //创建页眉模板
        public static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins, String groupName)
        {
            //获取页面大小
            SizeF pageSize = doc.PageSettings.Size;

            //创建PdfPageTemplateElement对象footerSpace，即页脚模板
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;

            //声明x,y两个float型变量
            float x = margins.Left;
            float y = 13;



            //groupName += "     第___________考场";



            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", 10f), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            footerSpace.Graphics.DrawString(groupName, font, PdfBrushes.Black, x, y, format);




            var rightText = "抽签顺序表";

            SizeF size = font.MeasureString(rightText);
            footerSpace.Graphics.DrawString(rightText, font, PdfBrushes.Black, pageSize.Width - size.Width - x, y, format);


            //在footerSpace中绘制线段
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.1f);
            footerSpace.Graphics.DrawLine(pen, x, y + 15, pageSize.Width - x, y + 15);

            //在footerSpace中绘制当前页码和总页码
            return footerSpace;
        }


        // 创建页脚模板
        public static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins, Group groupData)
        {
            //获取页面大小
            SizeF pageSize = doc.PageSettings.Size;

            //创建PdfPageTemplateElement对象footerSpace，即页脚模板
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;

            //声明x,y两个float型变量
            float x = margins.Left;
            float y = 0;

            //在footerSpace中绘制线段
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);

            //在footerSpace中绘制文字
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", 10f), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);




            //时间计算规则 只要 结束时间是12点以及之后都是下午场次
            DateTime beginDT = Util.CTime.GetDateTime(Convert.ToInt32(groupData.begin_time));

            var groupBeginTime = string.Format("{0:yyyy-MM-dd}", beginDT) + "   "+ groupData.time_period;


            String footerText1 = groupBeginTime;

            SizeF size1 = font.MeasureString(footerText1);
            footerSpace.Graphics.DrawString(footerText1, font, PdfBrushes.Black, pageSize.Width - size1.Width - x, y, format);


            //在footerSpace中绘制当前页码和总页码
            PdfPageNumberField number = new PdfPageNumberField();
            PdfPageCountField count = new PdfPageCountField();
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "第{0}页/共{1}页", number, count);
            compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Top);
            SizeF size = font.MeasureString(compositeField.Text);
            var w = (pageSize.Width - size.Width) / 2;
            compositeField.Bounds = new RectangleF(w, y, size.Width, size.Height);
            compositeField.Draw(footerSpace.Graphics);

            //返回footerSpace
            return footerSpace;
        }
    }
}
